package club.xyes.zkh.ecommerce.web.api.controller;

import club.xyes.zkh.ecommerce.commons.dto.MinimalUserInfo;
import club.xyes.zkh.ecommerce.commons.entity.CommodityClassification;
import club.xyes.zkh.ecommerce.commons.entity.Shop;
import club.xyes.zkh.ecommerce.commons.util.TextUtil;
import club.xyes.zkh.ecommerce.commons.vo.GeneralCollectionResult;
import club.xyes.zkh.ecommerce.commons.vo.GeneralResult;
import club.xyes.zkh.ecommerce.service.CommodityClassificationService;
import club.xyes.zkh.ecommerce.service.CommodityService;
import club.xyes.zkh.ecommerce.service.ShopService;
import club.xyes.zkh.ecommerce.web.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * Create by 郭文梁 2019/4/17 0017 10:08
 * CommodityClassificationController
 * 商品分类相关数据访问控制器
 *
 * @author 郭文梁
 * @data 2019/4/17 0017
 */
@RestController
@RequestMapping("/api/commodity-classification")
@Api("商品分类相关接口")
public class CommodityClassificationController extends BaseController<CommodityClassification> {
    private final CommodityClassificationService service;
    private final ShopService shopService;
    private final CommodityService commodityService;

    @Autowired
    public CommodityClassificationController(CommodityClassificationService service,
                                             ShopService shopService,
                                             CommodityService commodityService) {
        super(service);
        this.service = service;
        this.shopService = shopService;
        this.commodityService = commodityService;
    }

    /**
     * 当前用户所拥有商家的商品分类数据
     *
     * @return CommodityClassification List
     */
    @GetMapping("/mine")
    @ApiOperation("用户所拥有的店铺的商品分类")
    public GeneralCollectionResult<CommodityClassification> mine() {
        @NotNull MinimalUserInfo userInfo = getRequiredUserInfo();
        List<CommodityClassification> classifications = service.findByUserId(userInfo.getUserId());
        return GeneralCollectionResult.ok(classifications);
    }

    /**
     * 创建商品分类
     *
     * @param classification 分类(name, orderNum)
     * @return GR
     */
    @ApiOperation("创建商品分类")
    @PostMapping("/")
    public GeneralResult<CommodityClassification> create(@RequestBody CommodityClassification classification) {
        if (classification == null ||
                TextUtil.isTrimedEmpty(classification.getName()) ||
                classification.getOrderNum() == null) {
            return GeneralResult.badRequest("缺少参数");
        }
        Shop shop = currentShop(shopService);
        CommodityClassification target = new CommodityClassification();
        target.setName(classification.getName());
        target.setOrderNum(classification.getOrderNum());
        target.setShopId(shop.getId());
        CommodityClassification res = service.save(target);
        return GeneralResult.ok(res);
    }

    /**
     * 删除商品分类
     *
     * @param id ID
     * @return GR
     */
    @DeleteMapping("/{id}")
    @ApiOperation("删除商品分类")
    public GeneralResult<CommodityClassification> delete(@PathVariable("id") Integer id) {
        CommodityClassification classification = service.get(id);
        if (classification == null) {
            return GeneralResult.notFound();
        }
        Shop shop = currentShop(shopService);
        if (!Objects.equals(shop.getId(), classification.getShopId())) {
            return GeneralResult.permissionDenied("您无权操作该数据");
        }
        if (commodityService.existsByClassificationId(classification.getId())) {
            return GeneralResult.badRequest("该分类不为空， 无法删除");
        }
        if (service.deleteById(classification.getId())) {
            return GeneralResult.ok(classification);
        } else {
            return GeneralResult.error("Unable to delete Classification for id " + id);
        }
    }

    /**
     * 更新分类信息
     *
     * @param id             分类ID
     * @param classification {orderNum, name}
     * @return GR
     */
    @PostMapping("/{id}")
    @ApiOperation("更新分类信息")
    public GeneralResult<CommodityClassification> update(@PathVariable("id") Integer id,
                                                         @RequestBody CommodityClassification classification) {
        if (classification == null
                || classification.getOrderNum() == null
                || TextUtil.isTrimedEmpty(classification.getName())) {
            return GeneralResult.badRequest("缺少参数");
        }
        CommodityClassification target = service.getRequired(id);
        Shop shop = currentShop(shopService);
        if (!Objects.equals(shop.getId(), target.getShopId())) {
            return GeneralResult.permissionDenied("您无权操作该数据");
        }
        target.setOrderNum(classification.getOrderNum());
        target.setName(classification.getName());
        CommodityClassification res = service.updateById(target);
        return GeneralResult.ok(res);
    }

    /**
     * 根据商铺ID查询商品分类
     *
     * @param shopId 商铺ID
     * @return GR
     */
    @GetMapping("/shop/{shopId}")
    @ApiOperation("根据商铺ID查询分类")
    public GeneralResult<?> findByShopId(@PathVariable("shopId") Integer shopId) {
        Shop shop = shopService.getRequired(shopId);
        List<CommodityClassification> classifications = service.findByShop(shop);
        return GeneralResult.ok(classifications);
    }

    /**
     * 统计当前商铺的分类商品数量
     *
     * @return GR
     */
    @GetMapping("/count")
    @ApiOperation("统计当前商铺的分类商品数量")
    public GeneralResult<Map<String, Long>> count() {
        Shop shop = currentShop(shopService);
        Map<String, Long> res = service.countCommodityByShop(shop);
        return GeneralResult.ok(res);
    }
}
